package org.example.demoarticle.Controller;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.example.demoarticle.Service.UserService;
import org.example.demoarticle.pojo.ResultByLogin;
import org.example.demoarticle.pojo.ResultByProFile;
import org.example.demoarticle.pojo.UpdatePassword;
import org.example.demoarticle.pojo.User;
import org.example.demoarticle.util.jwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/zut")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/profile")
    public ResultByProFile proFile(HttpServletRequest request){
        String jwt=request.getHeader("Authorization").substring("Bearer ".length()).trim();
        String username = jwtUtil.getUsernameByJwt(jwt);
        User user=userService.profile(username);
        if(user!=null) {
            return ResultByProFile.success(user.name,user.username);
        }
        return ResultByProFile.error("jwt校验失败");
    }
    @RequestMapping("/update-password")
    public ResultByLogin updatePassword(HttpServletRequest request,
                                        @RequestBody UpdatePassword updatePassword) {
        String jwt=request.getHeader("Authorization").substring("Bearer ".length()).trim();
        String username = jwtUtil.getUsernameByJwt(jwt);
        int ret=userService.updatePassword(username, updatePassword.getPassword(), updatePassword.getNewPassword());
        if(ret>0) {
            return ResultByLogin.success();
        }
        return ResultByLogin.error("修改失败");
    }
}
